package com.itheima.Xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.SAXParser;
import java.util.ArrayList;
import java.util.List;

public class Test {
    //了解 xml的解析使用第三方解析工具
    public static void main(String[] args) throws DocumentException {
        ArrayList<User> users = new ArrayList<>();
       //创建xml解析器对象SaxReader
        SAXReader saxReader = new SAXReader();

        //使用解析器对象把xml文件读取成Document对象
        Document document = saxReader.read("D:\\java-base-ws" +
                "\\JavaAdvanceProject" +
                "\\day15_Properties\\src\\com\\itheima\\Xml\\users.xml");
        Element roots = document.getRootElement();

        List<Element> userse = roots.elements("user");
        for (Element user : userse) {
            int age = Integer.parseInt(user.elementText("age"));
            String sex = user.attributeValue("sex");
            String address = user.attributeValue("address");

            Element names = user.element("name");
            String name = names.getText();
            int sum =  Integer.parseInt(names.attributeValue("num"));
            int id = Integer.parseInt(names.attributeValue("id"));

            User user1 = new User(id, sum, name, age, sex, address);
            users.add(user1);
        }
        System.out.println("解析完毕。。。");
        users.forEach(System.out::println);



        //解析操作


        //获取根元素
    }
}
